package com.ultrapower.ctool.xn_app;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hh.timeselector.timeutil.datedialog.DateListener;
import com.hh.timeselector.timeutil.datedialog.TimeConfig;
import com.hh.timeselector.timeutil.datedialog.TimeSelectorDialog;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.http.BaseActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddWangGeEvaluateActivity extends BaseActivity implements View.OnClickListener {
    private EditText result_add_wg_evaluate;//验收结果
    private TextView time_add_wg_evaluate;//评价时间
    private ImageView time_img_add_wg_evaluate;
    private EditText evaluate_add_wg_evaluate;//网格员评价
    private Button save_add_wg_evaluate;
    private Button cancle_add_wg_evaluate;
    private CustomApplication application;
    private Gson gson = new Gson();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT).show();
                    // TODO: 2017/5/21 修改null
                    //Intent intent = new Intent(AddWangGeEvaluateActivity.this, null);
                    //startActivity(intent);
                    finish();
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "系统出现异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_wang_ge_evaluate);
        init();
    }

    protected void init(){
        application = (CustomApplication) getApplication();
        result_add_wg_evaluate = (EditText)findViewById(R.id.result_add_wg_evaluate);
        time_add_wg_evaluate = (TextView)findViewById(R.id.time_add_wg_evaluate);
        time_img_add_wg_evaluate = (ImageView)findViewById(R.id.time_img_add_wg_evaluate);
        evaluate_add_wg_evaluate = (EditText)findViewById(R.id.evaluate_add_wg_evaluate);
        save_add_wg_evaluate = (Button)findViewById(R.id.save_add_wg_evaluate);
        cancle_add_wg_evaluate = (Button)findViewById(R.id.cancle_add_wg_evaluate);
        time_img_add_wg_evaluate.setOnClickListener(this);
        save_add_wg_evaluate.setOnClickListener(this);
        cancle_add_wg_evaluate.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.time_img_add_wg_evaluate:
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String date = sdf.format(new Date());
                showDatePickDialog(TimeConfig.YEAR_MONTH_DAY_HOUR_MINUTE, date);
                break;
            case R.id.save_add_wg_evaluate:
               String  evaluate=evaluate_add_wg_evaluate.getText().toString().trim();
                String evaluatetime=time_add_wg_evaluate.getText().toString().trim();
                String evaluateresult=result_add_wg_evaluate.getText().toString().trim();
                if("".equals(evaluatetime)){
                    Toast.makeText(getApplicationContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();

                }else{
                    final Map<String, String> map = new HashMap<String, String>();
                    map.put("evaluate", evaluate);
                    map.put("evaluatetime", evaluatetime + ":00");
                    map.put("evaluateresult", evaluateresult);
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            Response enttyperesponse = null;
                            try {
                                OkHttpClient client = new OkHttpClient();
                                FormBody.Builder builder = new FormBody.Builder()
                                        // TODO: 2017/5/21 String.valueOf(application.getLoginUser().getLogin_id())
                                        .add("userId", String.valueOf(application.getLoginUser().getLogin_id()));
                                for (Map.Entry<String, String> m : map.entrySet()) {
                                    builder.add(m.getKey(), m.getValue());
                                }
                                RequestBody resultRequestBody = builder.build();
                                Request enttypeRequest = new Request.Builder().url(ConfUtils.SERVICE_URL + "form/doAdd/c_aj_sjpj").post(resultRequestBody).build();
                                enttyperesponse = client.newCall(enttypeRequest).execute();
                                if (enttyperesponse.isSuccessful()) {
                                    String result = enttyperesponse.body().string();
                                    Map<String, Object> resultMap = gson.fromJson(result, Map.class);
                                    System.out.println(resultMap);
                                    if ((boolean) resultMap.get("success")) {

                                        Message message = new Message();
                                        message.what = 1;
                                        handler.sendMessage(message);


                                    } else {
                                        Message message = new Message();
                                        message.what = 0;
                                        handler.sendMessage(message);
                                    }
                                }
                            } catch (Exception e) {
                                Message message = new Message();
                                message.what = 0;
                                handler.sendMessage(message);
                                e.printStackTrace();
                            } finally {
                                if (enttyperesponse != null) enttyperesponse.close();
                            }
                        }
                    }).start();
                }
                break;
            case R.id.cancle_add_wg_evaluate:
                finish();
                break;
        }
    }

    private void showDatePickDialog(int type, String date) {
        TimeSelectorDialog dialog = new TimeSelectorDialog(this);
        //设置标题
        dialog.setTimeTitle("选择时间:");
        //显示类型
        dialog.setIsShowtype(TimeConfig.YEAR_MONTH_DAY_HOUR_MINUTE);
        //默认时间
        dialog.setCurrentDate(date);
        //隐藏清除按钮
        dialog.setEmptyIsShow(false);
        dialog.setDateListener(new DateListener() {
            @Override
            public void onReturnDate(String time, int year, int month, int day, int hour, int minute, int isShowType) {
                time_add_wg_evaluate.setText(time);
            }

            @Override
            public void onReturnDate(String empty) {
                time_add_wg_evaluate.setText(empty);
            }
        });
        dialog.show();
    }
}
